﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer;
using System.IO;

namespace ZWZ.Web.WebAdmin
{
    public partial class log : System.Web.UI.Page
    {
        protected const string FROANTLOGPATH = "/logs/site/";
        protected const string BACKLOGPATH = "/logs/admin/";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CtrList1.Columns = new List<UCtrs.ListColumn>()
                {
                    new UCtrs.ListColumn(){ Caption="文件", FieldName="Name", Width="auto", Style="text-align:left;"},
                    new UCtrs.ListColumn(){ Caption="日期", FieldName="CreationTime", DataFormat="{0:yyyy-MM-dd}", Width="100px"},
                    new UCtrs.ListColumn(){ Caption="大小", FieldName="Size", Width="60px", Style="text-align:right;"},
                    new UCtrs.ListColumn(){ Caption="查看", FieldName="View", Width="60px", ShowToolTip=false},
                };
                txtStart.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                txtEnd.Text = DateTime.Now.ToString("yyyy-MM-dd");
                BindData();
            }
        }
        protected void CtrList1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string name = e.CommandArgument.ToString();
                string fpath = DropDownList1.SelectedValue == "后台日志" ? BACKLOGPATH : FROANTLOGPATH;
                File.Delete(HttpContext.Current.Server.MapPath(fpath+name));
                BindData();
            }
        }
        protected void CtrList1_PageChanging(object src, PageChangingEventArgs e)
        {
            CtrList1.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }
        protected void BindData()
        {
            DateTime StartTime = Convert.ToDateTime(txtStart.Text);
            DateTime EndTime = Convert.ToDateTime(txtEnd.Text);
            string logfilepath = DropDownList1.SelectedValue == "后台日志" ? BACKLOGPATH : FROANTLOGPATH;
            var allfiles = new DirectoryInfo(HttpContext.Current.Server.MapPath(logfilepath)).GetFiles().Where(file => file.CreationTime >= StartTime && file.CreationTime < EndTime.AddDays(1));
            var files = allfiles.Skip((CtrList1.CurrentPageIndex - 1) * CtrList1.PageSize).Take(CtrList1.PageSize).Select(
                    file => new
                    {
                        file.Name,
                        file.CreationTime,
                        Size = (file.Length / (1000)).ToString("N") + " KB",
                        View = string.Format("<a href='{0}' target='_blank'>[查看]</a>", logfilepath + file.Name)
                    }
                ).ToList();
            CtrList1.RecordCount = allfiles.Count();
            CtrList1.DataSource = files;
            CtrList1.DataBind();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}